package April._22.answer.work3;

import java.io.*;

public class Main {
    public static void main(String[] args) {
        new Thread(new CopyFileRunnable("D:\\temp\\aa.jpg", "aa.jpg")).start();
        new Thread(new CopyFileRunnable("D:\\temp\\bb.jpg", "bb.jpg")).start();
        new Thread(new CopyFileRunnable("D:\\temp\\cc.jpg", "cc.jpg")).start();
    }
}

class CopyFileRunnable implements Runnable{
    File oldFile;
    File newFile;

    public CopyFileRunnable(String oldFile, String newFile) {
        this.oldFile = new File(oldFile);
        this.newFile = new File(newFile);
    }
    @Override
    public void run() {
        // 复制文件操作
        try(FileInputStream in = new FileInputStream(oldFile);
            FileOutputStream out = new FileOutputStream(newFile)) {
            // 边读边写
            int readCount;
            byte[] bytes = new byte[1024];
            // 用来做累加的
            double temp = 0;
            long length = oldFile.length();
            while ((readCount = in.read(bytes)) != -1) {
                // write
                out.write(bytes,0,readCount);
                temp += readCount;
                double persent = temp / length * 100;
                System.out.println(oldFile + "复制了:"+String.format("%.2f",persent)+"%");
            }
            System.out.println(oldFile + "复制完成!");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
